<?php
/**
 * 字体管理
 *
 * 字体管理controller
 *
 * @author    zhouchao <zhouchao@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
namespace App\Http\Controllers\Backstage;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\FontService\FontBackService as FontService;

class FontController extends Controller
{
    protected $fontService;
    public function __construct(FontService $fontService)
    {
        $this->fontService = $fontService;
    }

    /**
     * 获得字体列表
     * @param Request $request
     * @return $this
     */
    public function getList(){
        $fonts = $this->fontService->get_font_list(15);
        return view('backstage.fonts.list')
            ->with('fonts',$fonts);
    }

    /**
     * 添加
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function getAdd(){
        return view('backstage.fonts.add');
    }

    /**
     * 添加字体包
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function postAdd(Request $request){
        $this->validate($request,[
            'font_name' => 'required|max:50|unique:fonts,font_name',
            'font_image' => 'required|image|max:2048',
            'font_path' => 'required',
            'font_type' => 'required|integer',
            'price' => 'required|numeric'
        ]);
        $data = $request->all();
        unset($data['_token']);
        $flag = $this->fontService->save_font($data);
        return $flag ? redirect()->back()->with('success_message', '添加成功') : redirect()->back()->with('error_message', '添加失败');
    }

    /**
     * 更新字体
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function getUpdate($font_id){
        $font_id = intval($font_id);
        //获取类型详细信息
        $this->fontService->setParameter([
            'table' => 'fonts',
            'where' => [['font_id','=',$font_id],['state','=',1]],
            'select'=> ['font_id','font_name','font_image','font_type','price','created_at']
        ]);
        $font = $this->fontService->getById();
        if(empty($font)){
            return redirect()->back()->with('error_message', '未找到该字体');
        }
        $font->font_image = env('IMG_URL_STD_PREFIX').$font->font_image;
        $modify_price = $this->fontService->get_modify_price($font_id);
        return view('backstage.fonts.update')
            ->with('font',$font)
            ->with('modify_price',$modify_price);
    }

    /**
     * 更新字体
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function postUpdate(Request $request){
        $this->validate($request,[
            'font_id' => 'required|integer|exists:fonts,font_id,state,1',
            'font_name' => 'required|max:50|unique:fonts,font_name,'.$request->input('font_id').',font_id',
            'font_image' => 'image|max:2048',
            'font_path' => 'mimes:ttf',
            'font_type' => 'required|integer',
            'price' => 'required|numeric'
        ]);
        $data = $request->all();
        unset($data['_token']);
        //判断名称除本身之外是否是唯一的
        $flag = $this->fontService->update_font($data);
        return $flag ? redirect()->back()->with('success_message', '修改成功') : redirect()->back()->with('error_message', '修改失败');
    }

    /**
     * 删除
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function getDelete($font_id){
        $font_id = intval($font_id);
        $this->fontService->setParameter([
            'table' => 'fonts',
            'where' => [['font_id','=',$font_id],['state','=',1]],
            'select'=> ['font_id','font_name','font_image','font_type','price','is_default','created_at']
        ]);
        $font = $this->fontService->getById();
        if($font->is_default == 1){
            return redirect()->back()->with('error_message','默认字体不允许删除');
        }
        if(empty($font)){
            return redirect()->back()->with('success_message','删除成功');
        }
        $flag = $this->fontService->remove_font($font_id);
        return $flag ? redirect()->back()->with('success_message', '删除成功') : redirect()->back()->with('error_message', '删除失败');
    }
}
